package com.lc.cehui.ui.activity.mine;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.load.MultiTransformation;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.cehui.R;
import com.lc.cehui.app.AppActivity;
import com.lc.cehui.http.glide.GlideApp;
import com.lc.cehui.http.request.MyMemberShowApi;
import com.lc.cehui.http.request.MyRechargeApi;
import com.lc.cehui.http.response.MyMemberShowBean;
import com.lc.cehui.http.response.RegisterBean;
import com.lc.cehui.other.IntentKey;
import com.lc.cehui.ui.adapter.BannerViewPagerAdapter;
import com.lc.cehui.util.BigDecimalUtils;
import com.lc.cehui.util.SPUtil;
import com.lc.cehui.widget.CustPagerTransformer;
import com.lcodecore.tkrefreshlayout.utils.DensityUtil;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MemberActivity extends AppActivity {
    @BindView(R.id.iv_header_icon)
    AppCompatImageView ivHeaderIcon;
    @BindView(R.id.tv_tel)
    TextView tvTel;
    @BindView(R.id.tv_tag)
    TextView tvTag;
    @BindView(R.id.view_page)
    ViewPager viewPage;
    @BindView(R.id.tv_power)
    TextView tvPower;
    @BindView(R.id.recyclerView_top)
    RecyclerView recyclerViewTop;
    @BindView(R.id.tv_package)
    TextView tvPackage;
    @BindView(R.id.recyclerView_bottom)
    RecyclerView recyclerViewBottom;
    private BannerViewPagerAdapter bannerAdapter;
    private List<MyMemberShowBean.DataBean.GroupBean.PowerBean> powerList = new ArrayList<>();
    private BaseQuickAdapter<MyMemberShowBean.DataBean.GroupBean.PowerBean, BaseViewHolder> powerAdapter;
    private List<MyMemberShowBean.DataBean.GroupBean.PackageBean> packageList = new ArrayList<>();
    private BaseQuickAdapter<MyMemberShowBean.DataBean.GroupBean.PackageBean, BaseViewHolder> packageAdapter;
    private String pack_id;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_member;
    }

    @Override
    protected void initView() {

        recyclerViewTop.setLayoutManager(new GridLayoutManager(this,3));
        powerAdapter = new BaseQuickAdapter<MyMemberShowBean.DataBean.GroupBean.PowerBean, BaseViewHolder>(R.layout.item_qiye_top, powerList) {
            @Override
            protected void convert(@NonNull BaseViewHolder baseViewHolder, MyMemberShowBean.DataBean.GroupBean.PowerBean powerBean) {
                GlideApp.with(MemberActivity.this).load(powerBean.getPicurl()).apply(new RequestOptions().placeholder(R.drawable.image11).error(R.drawable.image11)).into((ImageView) baseViewHolder.getView(R.id.iv_menu_home));
               baseViewHolder.setText(R.id.tv_menu_title_home,powerBean.getWork_type());
            }
        };
        recyclerViewTop.setAdapter(powerAdapter);
        recyclerViewBottom.setLayoutManager(new GridLayoutManager(this,3));
        packageAdapter=new BaseQuickAdapter<MyMemberShowBean.DataBean.GroupBean.PackageBean, BaseViewHolder>(R.layout.item_package, packageList) {
            @Override
            protected void convert(@NonNull BaseViewHolder baseViewHolder, MyMemberShowBean.DataBean.GroupBean.PackageBean packageBean) {
                if (packageBean.isGrop()){
                    baseViewHolder.setBackgroundResource(R.id.ll_bg,R.drawable.juxing_yuanjiao_fen1);
                }else {
                    baseViewHolder.setBackgroundResource(R.id.ll_bg,R.drawable.juxing_yuanjiao_blue2);

                }
               baseViewHolder.setText(R.id.tv_months,packageBean.getTitle());
                baseViewHolder.setText(R.id.tv_price,BigDecimalUtils.div(packageBean.getPrice(),"1",0)+"");
                if (baseViewHolder.getPosition()!=0){
                    baseViewHolder.setText(R.id.tv_subtitle, "折合¥"+BigDecimalUtils.div(packageBean.getPrice(),packageBean.getMonths()+"",0)+"/月");
                }else {
                    baseViewHolder.setText(R.id.tv_subtitle, BigDecimalUtils.div(packageBean.getPrice(),"1",0)+"/月");
                }
            }
        };
        packageAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                for (MyMemberShowBean.DataBean.GroupBean.PackageBean bean :packageList){
                    bean.setGrop(false);
                }
                MyMemberShowBean.DataBean.GroupBean.PackageBean bean = packageList.get(position);
                bean.setGrop(!bean.isGrop());

                pack_id = packageList.get(position).getPack_id()+"";
                packageAdapter.notifyDataSetChanged();

            }
        });
        recyclerViewBottom.setAdapter(packageAdapter);
        my_member_show();
    }

    private void my_member_show() {

        EasyHttp.post(this)
                .api(new MyMemberShowApi().setUserid(SPUtil.get(this, IntentKey.USERID, 0)))
                .request(new HttpCallback<MyMemberShowBean>(this) {

                    @Override
                    public void onSucceed(MyMemberShowBean data) {
                        GlideApp.with(getActivity())
                                .load(data.getData().getUser().getPicurl())
                                .placeholder(R.mipmap.mine_default_avatar)
                                .error(R.mipmap.mine_default_avatar)
                                .transform(new MultiTransformation<>(new CenterCrop(), new CircleCrop()))
                                .into(ivHeaderIcon);
                        tvTel.setText(data.getData().getUser().getMobile());
                        tvTag.setText(data.getData().getUser().getTitle());
                        if (data.getData().getGroup()!=null){
                            tvPower.setText("专享"+data.getData().getGroup().get(0).getCount()+"大权益");
                            tvPackage.setText(data.getData().getGroup().get(0).getTitle()+"套餐推荐");
                            powerList.clear();
                            powerList.addAll(data.getData().getGroup().get(0).getPower());
                            powerAdapter.setNewData(powerList);
                            powerAdapter.notifyDataSetChanged();
                            packageList.clear();
                            packageList.addAll(data.getData().getGroup().get(0).getPackageX());
                            if (packageList.size()>0) {
                                packageList.get(0).setGrop(true);
                                pack_id = packageList.get(0).getPack_id() + "";
                            }
                            packageAdapter.setNewData(packageList);
                            packageAdapter.notifyDataSetChanged();
                        }
                        viewPage.setAdapter(bannerAdapter = new BannerViewPagerAdapter(viewPage));
                        viewPage.setPageMargin(DensityUtil.dp2px(getActivity(), 12));
                        viewPage.setPageTransformer(false, new CustPagerTransformer());
                        bannerAdapter.addData(data.getData().getGroup());
                        viewPage.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                            @Override
                            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                            }

                            @Override
                            public void onPageSelected(int position) {
                                tvPower.setText("专享"+data.getData().getGroup().get(position).getCount()+"大权益");
                                tvPackage.setText(data.getData().getGroup().get(position).getTitle()+"套餐推荐");
                                powerList.clear();
                                powerList.addAll(data.getData().getGroup().get(position).getPower());
                                powerAdapter.setNewData(powerList);
                                powerAdapter.notifyDataSetChanged();
                                packageList.clear();
                                packageList.addAll(data.getData().getGroup().get(position).getPackageX());
                                if (packageList.size()>0) {
                                    packageList.get(0).setGrop(true);

                                pack_id=packageList.get(0).getPack_id()+"";
                                }
                                packageAdapter.setNewData(packageList);
                                packageAdapter.notifyDataSetChanged();

                            }

                            @Override
                            public void onPageScrollStateChanged(int state) {

                            }
                        });
                    }
                });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.sb_agree, R.id.bt_submit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.sb_agree:
                startActivity(new Intent(MemberActivity.this, AgreeActivity.class).putExtra("id","16").putExtra("title","会员协议"));
                break;
            case R.id.bt_submit:
                my_recharge();
                break;
        }
    }

    private void my_recharge() {
        EasyHttp.post(this)
                .api(new MyRechargeApi().setUserid(SPUtil.get(this, IntentKey.USERID, 0)).setPack_id(pack_id))
                .request(new HttpCallback<RegisterBean>(this) {

                    @Override
                    public void onSucceed(RegisterBean data) {

                    }
                });
                    }

}
